<?php
class user_model extends CI_Model{
	private $userid;
	private $fullname;
	private $password;
	private $beforepassword;
	private $gradeid;
	private $createdby;
	private $createdon;
	private $lastupdateby;
	private $lastupdateon;
	private $active;

	function setUserId($id){
		$this->userid = $id;
	}

	function getUserId(){
		return $this->userid;
	}

	function setFullName($fname){
		$this->fullname = $fname;
	}

	function getFullName(){
		return $this->fullname;
	}

	function setPassword($passwd){
		$this->password = $passwd;
	}

	function getPassword(){
		return $this->password;
	}

	function setBeforePassword($bpasswd){
		$this->beforepassword = $bpasswd;
	}

	function getBeforePassword(){
		return $this->beforepassword;
	}

	function setGrade($grad){
		$this->gradeid = $grad;
	}

	function getGrade(){
		return $this->gradeid;
	}

	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}

	function getCreatedBy(){
		return $this->createdby;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}

	function getCreatedOn(){
		return $this->createdon;
	}

	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}

	function getLastUpdateBy(){
		return $this->lastupdateby;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}

	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = strval($active_val);
	}

	function getActive(){
		return $this->active;
	}

	// Call the module constructor
	function __construct(){
		parent::__construct();
	}

	function insert(){
		$data = array(
			'userid' => $this->getUserId(),
			'fullname' => $this->getFullName(),
			'password' => $this->getPassword(),
			'beforepassword' => $this->getBeforePassword(),
			'gradeid' => $this->getGrade(),
			'createdby' => $this->getCreatedBy(),
			'createdon' => $this->getCreatedOn(),
			'lastupdateby' => 'null',
			'lastupdateon' => 'null',
			'active' => '1'						
		);
		$this->db->insert('user',$data);
	}

	function update(){
		$data = array();
		if($this->getFullName()){
			$data['fullname'] = $this->getFullName();
		}
		if($this->getPassword()){
			$data['password'] = $this->getPassword();
		}
		if($this->getBeforePassword()){
			$data['beforepassword'] = $this->getBeforePassword();
		}
		if($this->getGrade()){						
			$data['gradeid'] = strval($this->getGrade());
		}
		if($this->getActive()){
			$data['active'] = strval($this->getActive());
		}
		if($this->getLastUpdateBy()){
			$data['lastupdateby'] = $this->getLastUpdateBy();
		}
		if($this->getLastUpdateOn()){
			$data['lastupdateon'] = $this->getLastUpdateOn();
		}
		$this->db->where(array('userid' => $this->getUserId()));
		$this->db->update('user',$data);
		//var_dump($this->db->last_query());
	}

	function getLogin(){
		$query = $this->db->query("select userid,fullname,gradeid from user where userid=? and password=? and active=1 limit 1",array($this->getUserId(),$this->getPassword()));
		if($query->num_rows() > 0){
			foreach($query->result() as $rows){
				$this->setUserId($rows->userid);
				$this->setFullName($rows->fullname);
				$this->setGrade($rows->gradeid);
			}
			return true;
		}else{
			return false;
		}
	}
	
	function getUserList(){
		$this->db->select('u.userid,u.fullname,u.password,g.gradeid,g.description')
		->join('usergrade g','u.gradeid=g.gradeid')->where(array('u.active'=>'1','g.active'=>'1'));
		$query = $this->db->get('user u');
		return $query->result();
	}
	
	function getUserByUserId(){
		$this->db->select('count(userid) retval')->where(array('userid' => $this->getUserId()));
		$query = $this->db->get('user');
		return $query->result();
	}
	
	function getUserByUserIds(){
		$this->db->where(array('UserId' => $this->getUserId()));
		$query = $this->db->get('user');
		return $query->result();
	}
}